package edu.polsl.bio.str.manager.service;

import edu.polsl.bio.str.common.api.NodeManagementService;
import edu.polsl.bio.str.common.exception.ManagerException;
import edu.polsl.bio.str.manager.logic.NodeManagementBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * //todo class description
 * <p/>
 * Created on: Jun 13, 2011 12:50:58 PM <br/>
 * <a href="http://www.polsl.pl">www.polsl.pl</a>
 *
 * @author lukasz.olczak@polsl.pl
 */
public class NodeManagementServiceImpl extends AbstractService implements NodeManagementService {
  /**
   * slf4j Logger.
   */
  private static final Logger log = LoggerFactory.getLogger(NodeManagementServiceImpl.class.getName());

  private NodeManagementBean nodeManagementBean;

  @Override
  public long attachNode(String nodeIp, int noTokens) throws ManagerException {
    try {
      return nodeManagementBean.attachNode(nodeIp, noTokens);
    } catch (Exception e) {
      log.error("Error while attaching node " + nodeIp, e);
      throw createManagerException(e);
    }
  }

  @Override
  public void detachNode(long nodeId) throws ManagerException {
    try {
      nodeManagementBean.detachNode(nodeId);
    } catch (Exception e) {
      log.error("Error while detaching node " + nodeId, e);
      throw createManagerException(e);
    }
  }

  @Override
  public void heartBeat(long nodeId) throws ManagerException {
    try {
      nodeManagementBean.heartBeat(nodeId);
    } catch (Exception e) {
      log.error("Error while collecting heartbeat ", e);
      throw createManagerException(e);
    }
  }

  public void setNodeManagementBean(NodeManagementBean nodeManagementBean) {
    this.nodeManagementBean = nodeManagementBean;
  }
}
